﻿using Shake.Domain.Filters;
using Shake.Domain.Maps;
using Shake.Domain.Models;
using Shake.Framework.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shake.Business.Services
{
    public class CertificateService : ICertificateService
    {
        public IRepository Repository { get; set; }

        public List<Certificate> GetCertificate(CertificateFilter certificateFilter)
        {
            var factory = Repository.CreateSessionFactory();
            using (var session = factory.GetCurrentSession())
            {
                var query = session.QueryOver<Certificate>();
                if (!string.IsNullOrEmpty(certificateFilter.UserId))
                {
                    query = query.Where(t => t.UserId == certificateFilter.UserId);
                }

                var results = query.List().ToList();
                return results;
            }
        }

        public void SaveCertificate(List<Certificate> certificates)
        {
            foreach (var certificate in certificates)
            {
                if (string.IsNullOrEmpty(certificate.Id))
                {
                    certificate.Id = Guid.NewGuid().ToString("N");
                    Repository.InsertMany(certificates);
                }
                else
                {
                    Repository.Update(certificate);
                }
            }
        }

        public void RemoveCertificate(List<Certificate> certificates)
        {
            Repository.DeleteMany(certificates);
        }
    }
}
